# PowerShell Deploy Script
# For compiling frontend project and deploying to Linux server

# Set error handling
$ErrorActionPreference = "Stop"

# Server configuration
$ServerIP = "8.140.204.4"
$Username = "root"
$Password = "Q83ZGm0EfibVmv4"
$RemoteDir = "/mnt/app/docker/nginx/"
$RemoteHtmlDir = "/mnt/app/docker/nginx/html"
$LocalZipFile = "apps/web-antd/dist.zip"

Write-Host "Starting deployment process..." -ForegroundColor Green

try {
    # Step 1: Execute build command
    Write-Host "Step 1: Executing build command pnpm build:antd" -ForegroundColor Yellow
    pnpm build:antd
    
    if (-not (Test-Path $LocalZipFile)) {
        throw "Build failed: Could not find generated zip file $LocalZipFile"
    }
    Write-Host "Build completed, generated file: $LocalZipFile" -ForegroundColor Green
    
    # Step 2: Upload file to Linux server
    Write-Host "Step 2: Uploading file to Linux server" -ForegroundColor Yellow
    
    # Use pscp command to upload file (PuTTY tool)
    $pscpCommand = "pscp -pw $Password $LocalZipFile ${Username}@${ServerIP}:${RemoteDir}"
    Write-Host "Executing command: pscp -pw ****** $LocalZipFile ${Username}@${ServerIP}:${RemoteDir}"
    
    # Check if pscp command exists
    if (Get-Command "pscp" -ErrorAction SilentlyContinue) {
        Invoke-Expression $pscpCommand
        Write-Host "File upload completed" -ForegroundColor Green
    } else {
        Write-Host "pscp command not found, trying scp..." -ForegroundColor Yellow
        
        # Try using scp command
        if (Get-Command "scp" -ErrorAction SilentlyContinue) {
            Write-Host "Please manually enter password to complete file upload:" -ForegroundColor Cyan
            scp $LocalZipFile "${Username}@${ServerIP}:${RemoteDir}"
            Write-Host "File upload completed" -ForegroundColor Green
        } else {
            Write-Host "Error: Neither scp nor pscp command found" -ForegroundColor Red
            Write-Host "Please install OpenSSH client or PuTTY tools" -ForegroundColor Red
            Write-Host "Manual upload command: scp $LocalZipFile ${Username}@${ServerIP}:${RemoteDir}" -ForegroundColor Cyan
            Write-Host "Password: $Password" -ForegroundColor Cyan
            Read-Host "Please execute the above command manually and press Enter to continue"
        }
    }
    
    # Steps 3 and 4: Execute cleanup and extraction operations on server
    Write-Host "Steps 3-4: Cleaning directory and extracting files" -ForegroundColor Yellow
    
    # SSH commands
    $sshCommands = "rm -rf $RemoteHtmlDir/* && unzip -o ${RemoteDir}dist.zip -d $RemoteHtmlDir && rm -f ${RemoteDir}dist.zip && echo 'Deployment completed'"
    
    # Check if plink command exists (PuTTY)
    if (Get-Command "plink" -ErrorAction SilentlyContinue) {
        $plinkCommand = "plink -pw $Password ${Username}@${ServerIP} `"$sshCommands`""
        Write-Host "Executing SSH commands..." -ForegroundColor Cyan
        Invoke-Expression $plinkCommand
        Write-Host "Server operations completed" -ForegroundColor Green
    } elseif (Get-Command "ssh" -ErrorAction SilentlyContinue) {
        Write-Host "Please manually enter password to execute SSH commands:" -ForegroundColor Cyan
        ssh "${Username}@${ServerIP}" $sshCommands
        Write-Host "Server operations completed" -ForegroundColor Green
    } else {
        Write-Host "Error: Neither ssh nor plink command found" -ForegroundColor Red
        Write-Host "Please install OpenSSH client or PuTTY tools" -ForegroundColor Red
        Write-Host "Manual SSH command: ssh ${Username}@${ServerIP}" -ForegroundColor Cyan
        Write-Host "Password: $Password" -ForegroundColor Cyan
        Write-Host "Then execute: $sshCommands" -ForegroundColor Cyan
        Read-Host "Please execute the above commands manually and press Enter to continue"
    }
    
    Write-Host "Deployment process completed!" -ForegroundColor Green
    
} catch {
    Write-Host "Error occurred during deployment: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# Usage instructions
Write-Host ""
Write-Host "Usage Instructions:" -ForegroundColor Cyan
Write-Host "1. Ensure pnpm is installed" -ForegroundColor White
Write-Host "2. Ensure OpenSSH client or PuTTY tools are installed" -ForegroundColor White
Write-Host "3. Run in PowerShell: .\deploy.ps1" -ForegroundColor White
